/**
 * @description: 重置操作
 * @file: reset.ts
 * @author: Evan
 * @date: 2025.07.17 15:43:15
 * @week: 周四
 * @version: V
 */

import { Message } from '@/utils/element'
import { Scene } from '../class'
import * as MD from '../data/methods'

const { THREE, Hooks } = MD

const { centerBoxHelper } = Hooks.useMaterial()

type ST = InstanceType<typeof Scene>

export function reset(core: ST) {
  return {
    add() {
      const gui = core.gui
      const option = {
        viewReset: () => core.cameraViewReset(),
        rotation: 0,
        grid: true,
        scale: 1,
        timeScale: 1,
        changeAnimate: () => {
          core.modelGroup?.children.forEach((model: MD.THREE.Object3D) => {
            if (model.userData.__action__) {
              const names = model.userData.__action_names__ || ['']
              const len = names.length
              let name = names[0]
              for (let key in model.userData.__action__) {
                const runing = model.userData.__action__[key].isRunning()
                if (runing) {
                  name = key
                  break
                }
              }
              let index = names.findIndex((el: string) => el === name)
              index++
              if (index > len - 1) index = 0
              name = names[index]
              if (name) {
                model.userData.__mixer__?.stopAllAction()
                Message(`当前执行动画【${name}】，第${index + 1}个，共${len}个动画`)
                model.userData.__action__[name].play()
              }
            }
          })
        },
        stopAnimate: () => {
          core.modelGroup?.children.forEach((model: any) => {
            model.userData.__mixer__?.stopAllAction()
          })
        },
        center: () => this.modelCenter()
      }

      const group = gui.addFolder('重置')
      group.add(option, 'viewReset').name('视角重置')
      group
        .add(option, 'grid')
        .name('网格显示')
        .onChange((v: boolean) => {
          if (core.grid) core.grid.visible = v
          const fork = core.scene.getObjectByProperty('_isGridFork_', true)
          if (fork) fork.visible = v
        })
      group.add(core.controls, 'autoRotate').name('自动旋转')
      group.add(core.guiOpts, 'insetAnimate').name('入场动画')
      group
        .add(option, 'rotation', 0, 360)
        .name('旋转')
        .onChange((e: number) => {
          core.modelGroup?.children.forEach((model: MD.THREE.Object3D) => {
            const r = (Math.PI / 180) * e
            model.rotation.set(0, r, 0)
          })
        })
      group
        .add(option, 'scale', 0.001, 100)
        .step(0.01)
        .name('缩放')
        .onChange((e: number) => {
          core.modelGroup?.children.forEach((model: MD.THREE.Object3D) => {
            model.scale.set(e, e, e)
          })
        })
      group
        .add(option, 'timeScale', 0.1, 10)
        .name('动画速度')
        .onChange((e: number) => {
          core.modelGroup?.children.forEach((model: MD.THREE.Object3D) => {
            if (model.userData.__action__) {
              Object.keys(model.userData.__action__).forEach(key => {
                model.userData.__action__[key].timeScale = e
              })
            }
          })
        })
      group.add(option, 'changeAnimate').name('切换动画')
      group.add(option, 'stopAnimate').name('停止动画')
      group.add(option, 'center').name('居中')

      group.close()
    },

    // 模型居中
    modelCenter() {
      core.helperGroup?.children.forEach((el: any) => {
        if (el.type === 'BoxHelper') {
          core.disposeObj(el)
          core.helperGroup?.remove(el)
        }
      })

      const list: any[] = []
      core.modelGroup?.children.forEach((el: any) => {
        const { helper, center } = centerBoxHelper(el, 0xff0000)
        core.modules.mian?.addHelper(helper)
        // @ts-ignore
        center.name = el.name
        const { x, y, z } = el.position
        // 将模型进行偏移居中
        el.position.set(x - center.x, y, z - center.z)
        const group = new THREE.Group()
        let model = el.clone()
        group.add(model)
        group.animations = model.animations
        list.push(el)
        core.modules.mian?.addModel(model)
      })
      list.forEach(el => {
        core.disposeObj(el)
        core.modelGroup?.remove(el)
      })
      console.log(core)
    }
  }
}
